﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities.IRepository;
using Entities;
using BLToolkit.Data;
using BLToolkit.Data.Linq;

namespace DAL.Admin
{
    public class BLTTaiKhoanRepository :ConnectionStringBL, ITaiKhoanRepository
    {
        //private DbManager db = new DbManager("QuanLiBongDa");



        public int ThemTaiKhoan(TaiKhoan taikhoan)
        {
            try
            {
                var kq = db.InsertWithIdentity(taikhoan);
                // đảm bảo thành công
                db.CommitTransaction();
                return Convert.ToInt32(kq);

            }
            catch (Exception)
            {
                // đảo bảo thất bại lui lại bước trước khi thực hiên  
                db.RollbackTransaction();
                return -1;
            }
        }

        public bool CapNhatTaiKhoan(TaiKhoan taikhoan)
        {
            try
            {
                int kq = db.Update(taikhoan);
                db.CommitTransaction();
                return kq > 0;


            }
            catch (Exception)
            {

                db.RollbackTransaction();
                return false;
            }
        }

        public bool XoaTaiKhoan(TaiKhoan taikhoan)
        {
            try
            {
                int kq = db.Delete(taikhoan);
                db.CommitTransaction();
                return kq > 0;
            }
            catch (Exception)
            {

                db.RollbackTransaction();
                return false;
            }
        }

        public bool CapNhatPhanQuyen(string Us, int phanquyen)
        {
            try
            {
                int kq = db.GetTable<TaiKhoan>().Where(x => x.UserName == Us)
                    .Set(x => x.PhanQuyen, phanquyen)
                    .Update();

                db.CommitTransaction();
                return kq > 0;


            }
            catch (Exception)
            {

                db.RollbackTransaction();
                return false;
            }
        }

        public TaiKhoan LayTheoId(int id)
        {
            return db.GetTable<TaiKhoan>()
           .SingleOrDefault(x => x.MaTaiKhoan == id);
        }

        public TaiKhoan LayChiTietTheoId(int id)
        {
            TaiKhoan taikhoan = LayChiTietTheoId(id);
            if (taikhoan != null)
                taikhoan.Ds_QLTaiKhoans = db.GetTable<QuanLiTaiKhoan>()
                    .Where(x => x.MaTaiKhoan == id)
                    .ToList();
            return taikhoan;
                 
        }

        public List<TaiKhoan> LayTatCa()
        {
           return db.GetTable<TaiKhoan>().ToList();
        }

        public TaiKhoan LayTheoUsername(string us)
        {
           return db.GetTable<TaiKhoan>().Where(x => x.UserName==us).SingleOrDefault();
        }
    }
}
